package com.fand.platform.common.support;

import java.util.List;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import com.fand.platform.authority.dto.NavigationDto;
import com.fand.platform.authority.service.AuthorityService;
import com.fand.platform.configuration.dto.DictionaryDto;
import com.fand.platform.configuration.dto.DictionaryTyDto;
import com.fand.platform.configuration.dto.SiteDto;
import com.fand.platform.configuration.dto.VariableDto;
import com.fand.platform.configuration.service.ConfigurationService;

/**
 * 初始化数据支持
 * 
 * @author yaoHT
 */
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class InitDataSupport {
	private AuthorityService authorityService;

	private ConfigurationService configurationService;

	private DictionaryTyDto dictionaryTy;

	private SiteDto site;

	private List<VariableDto> variableList;

	private Boolean initData;

	public void init() {
		if (!BooleanUtils.isTrue(this.initData)) {
			return;
		}

		if (this.dictionaryTy != null) {
			this.initDictionaryTy(this.dictionaryTy);
		}

		if (this.site != null) {
			this.initSite(this.site);
		}

		if (!CollectionUtils.isEmpty(this.variableList)) {
			this.initVariable(this.variableList);
		}
	}

	/**
	 * 初始化：字典
	 * 
	 * @param saveDictionaryList
	 *            字典集合
	 * @param typeId
	 *            类别标识
	 */
	private void initDictionary(final List<DictionaryDto> saveDictionaryList, final String typeId) {
		if (CollectionUtils.isEmpty(saveDictionaryList)) {
			return;
		}

		for (final DictionaryDto saveDictionary : saveDictionaryList) {
			saveDictionary.setTypeId(typeId);

			this.configurationService.getDictionaryService().saveDictionaryByIdOrCodeForDictionary(saveDictionary);
		}
	}

	/**
	 * 初始化：字典类型
	 * 
	 * @param saveDictionaryTy
	 *            字典类型对象
	 */
	private void initDictionaryTy(final DictionaryTyDto saveDictionaryTy) {
		final DictionaryTyDto dictionaryTy = this.configurationService.getDictionaryService()
				.saveDictionaryTyByIdOrCodeForDictionaryTy(saveDictionaryTy);

		this.initDictionary(saveDictionaryTy.getDictionaryList(), dictionaryTy.getId());

		if (CollectionUtils.isEmpty(saveDictionaryTy.getChilds())) {
			return;
		}

		for (final DictionaryTyDto childDictionaryTy : saveDictionaryTy.getChilds()) {
			childDictionaryTy.setParentId(dictionaryTy.getId());

			this.initDictionaryTy(childDictionaryTy);
		}
	}

	/**
	 * 初始化：导航
	 * 
	 * @param saveNavigation
	 *            导航对象
	 */
	private void initNavigation(final NavigationDto saveNavigation) {
		if (StringUtils.isBlank(saveNavigation.getTypeId())) {
			final String typeId = this.configurationService.getDictionaryService()
					.readDictionaryByTypeCoEqCodeForId("NAVIGATION_TY", saveNavigation.getTypeCo());

			saveNavigation.setTypeId(typeId);
		}

		final NavigationDto navigation = this.authorityService.getNavigationService()
				.saveNavigationByIdOrCodeForNavigation(saveNavigation);

		if (CollectionUtils.isEmpty(saveNavigation.getChilds())) {
			return;
		}

		for (final NavigationDto childNavigation : saveNavigation.getChilds()) {
			childNavigation.setSiteId(saveNavigation.getSiteId());

			childNavigation.setStateId(saveNavigation.getStateId());

			childNavigation.setTypeCo(saveNavigation.getTypeCo());

			childNavigation.setTypeId(saveNavigation.getTypeId());

			childNavigation.setParentId(navigation.getId());

			this.initNavigation(childNavigation);
		}
	}

	/**
	 * 初始化：站
	 * 
	 * @param saveSite
	 *            站对象
	 */
	private void initSite(final SiteDto saveSite) {
		final SiteDto site = this.configurationService.getSiteService().saveSiteByIdOrCodeForSite(saveSite);

		if (!CollectionUtils.isEmpty(saveSite.getNavigationList())) {
			final String stateId = this.configurationService.getDictionaryService()
					.readDictionaryByTypeCoEqCodeForId("NavigationState", "1");

			for (final NavigationDto navigation : saveSite.getNavigationList()) {
				navigation.setSiteId(site.getId());

				navigation.setStateId(stateId);

				this.initNavigation(navigation);
			}
		}
	}

	/**
	 * 初始化：变量
	 * 
	 * @param variableList
	 *            变量集合
	 * @param siteId
	 *            站标识
	 */
	private void initVariable(final List<VariableDto> variableList) {
		if (CollectionUtils.isEmpty(variableList)) {
			return;
		}

		for (final VariableDto variable : variableList) {
			final String typeId = this.configurationService.getDictionaryService()
					.readDictionaryByTypeCoEqCodeForId("VariableType", variable.getTypeCo());

			variable.setTypeId(typeId);

			this.configurationService.getVariableService().saveVariableByIdOrCodeForVariable(variable);
		}
	}
}